﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebApi.Models;

namespace WebApi.Bill.Biz
{
    public class BizCommentInfo : BizBase
    {
        readonly string tb = "CommentInfo";
        readonly string pk = "commentID";

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(CommentInfoModel model)
        {
            model.userCommentDate = DateTime.Now;
            return Da.InsertModel<CommentInfoModel>(model);
        }

        public int AddModel(CommentInfoModel model)
        {
            model.userCommentDate = DateTime.Now;
            return Da.InsertModel<CommentInfoModel>(model);
        }
        #endregion

        #region 更新
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(CommentInfoModel model)
        {
            return Da.UpdateModel(model);
        }
        #endregion

        #region GetList
        /// <summary>
        /// 获取信息列表
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataTable GetList(string sql)
        {
            return Da.GetList(sql);
        }

        public List<CommentInfoModel> GetListBySql(string sql)
        {
            return Da.GetModelList<CommentInfoModel>(sql);
        }

        public List<CommentInfoModel> GetListModel()
        {
            return Da.GetModelList<CommentInfoModel>();
        }
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="pageNum">当前页码</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="recordCount">总列数</param>
        /// <returns></returns>
        public List<CommentInfoModel> GetPageList(dynamic data, ref int recordCount, ref int totalPage)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append($"select * from {this.tb} where 1=1 ");
            JObject joFilters = FiltersSql.GetQueryCond(data);
            sb.Append(joFilters["sqlAppend"].ToString());
            sb.Append($" order by userCommentDate desc");
            int pageNum = Convert.ToInt32(joFilters["pageNum"]);
            int pageSize = Convert.ToInt32(joFilters["pageSize"]);
            return Da.PageList<CommentInfoModel>(sb.ToString(), pageNum, pageSize, ref recordCount, ref totalPage);
        }
        #endregion

        #region Get
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public CommentInfoModel Get(int code)
        {
            return Da.GetModel<CommentInfoModel>(x => x.userID == code);
        }
        #endregion

        #region Delete 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int Delete(string code)
        {
            return Da.Delete(this.tb, this.pk, code);
        }
        #endregion

    }
}
